from resource_management.core.resources.system import Execute
from resource_management.libraries.script import Script
from resource_management.libraries.functions import get_user_call_output
from resource_management.core.resources.system import Directory
from resource_management.core.resources.system import File
from resource_management.core.source import InlineTemplate
from resource_management.libraries.functions.check_process_status import check_process_status
from resource_management.core.exceptions import ClientComponentHasNoStatus
from resource_management.libraries.functions.show_logs import show_logs

import os





class Console(Script):
    pid_file=''
    def install(self, env):
        import params
        env.set_params(params)
        self.install_packages(env)
        Directory([
            params.install_dir, params.mlsql_log_dir, params.mlsql_pid_dir

        ],
            owner=params.mlsql_user,
            group=params.user_group,
            mode=0755,
            create_parents=True)
        if not os.path.exists(params.install_dir + '/mlsql-console'):
            import time
            if not os.path.exists(params.download_console_file):
                Execute(format('rm -f ' + params.download_console_file), user=params.mlsql_user)
                Execute(format('wget ' + params.download_url_console + ' -O ' + params.download_console_file),
                        user=params.mlsql_user)
            Execute(format('rm -rf '+ params.install_dir +'/mlsql-console*'), user=params.mlsql_user)
            # install mlsql

            tar_cmd = format('tar zxvf '+ params.download_console_file + '  -C ' + params.install_dir)
            Execute(tar_cmd, user=params.mlsql_user)
            # Execute('tar zxvf /tmp/mlsql-console.tar.gz   -C /opt/mlsql' ,user=params.mlsql_user)
            time.sleep(5)
            Execute(format('ln -s '+ params.install_dir +'/mlsql-console* '+ params.install_dir +'/mlsql-console'), user=params.mlsql_user)
            # Execute("mysql -uroot -p123456 -e 'show databases;'", user=params.mlsql_user)
            # Execute("mysql -uroot -p123456 -e  'drop database if exists mlsql_console;'", user=params.mlsql_user)
            # Execute("mysql -uroot -p123456 -e  'create database if not exists mlsql_console;'", user=params.mlsql_user)
            # Execute("mysql -uroot -p123456 -Dmlsql_console < /opt/mlsql/mlsql-console/db.sql", user=params.mlsql_user)

            if params.skip_console_script_inport == '0':
                if params.skip_modify_sql_script_datatype_256to255 == '0':
                    Execute(format("sed -i 's/`name` varchar(256)/`name` varchar(255)/' "+ params.install_dir +"/mlsql-console/console-db.sql"), user=params.mlsql_user)
                db_show_cmd = format("mysql -h " + params.db_url + " -u" + params.db_user + " -p" + params.db_password + " -e 'show databases;'")
                Execute(db_show_cmd, user=params.mlsql_user)
                # db_drop_cmd = format("mysql -u" + params.db_user + " -p" + params.db_password + " -e  'drop database if exists "+ params.console_db + ";'")
                # Execute(db_drop_cmd, user=params.mlsql_user)
                db_create_cmd = format("mysql -h " + params.db_url + " -u" + params.db_user + " -p" + params.db_password + " -e  'create database if not exists "+ params.console_db + ";'")
                Execute(db_create_cmd, user=params.mlsql_user)
                db_import_cmd = format("mysql -h " + params.db_url + " -u" + params.db_user + " -p" + params.db_password + " -D"+ params.console_db + " < "+ params.install_dir +"/mlsql-console/console-db.sql")
                Execute(db_import_cmd, user=params.mlsql_user)


    def configure(self, env, isInstall=False):
        import params
        import status_params
        env.set_params(params)
        env.set_params(status_params)
        Directory([
            params.install_dir, '/var/log/mlsql', params.mlsql_pid_dir,
            params.install_dir + '/mlsql'
        ],
            owner=params.mlsql_user,
            group=params.user_group,
            mode=0755,
            create_parents=True)
        File(os.path.join( params.install_dir + '/mlsql-console', "application.docker.yml"),
             content=InlineTemplate( params.console_conf_application_docker_yml),
             owner=params.mlsql_user,
             group=params.user_group
             )
        File(os.path.join( params.install_dir + '/mlsql-console', "start-default.sh"),
             content=InlineTemplate(params.console_conf_start_default_sh),
             owner=params.mlsql_user,
             group=params.user_group
             )
        File(os.path.join( params.install_dir + '/mlsql-console', "start.sh"),
             content=InlineTemplate(params.console_conf_start_sh),
             owner=params.mlsql_user,
             group=params.user_group
             )
        File(os.path.join( params.install_dir + '/mlsql', "mlsql-console.sh"),
             content=InlineTemplate( params.mlsql_console_script),
             owner=params.mlsql_user,
             group=params.user_group
             )

    
    def stop(self, env):
        import params
        import status_params
        Execute('sh '+ params.install_dir +'/mlsql/mlsql-console.sh stop', user=params.mlsql_user)
    
        
    def start(self, env):
        import params
        import time
        import os
        import subprocess
        env.set_params(params)
        self.configure(env)
        # Execute('service mlsql-cluster start',user=params.mysql_user)
        # time.sleep(5)
        # Execute('service mlsql-cluster status', user=params.mysql_user)
        console_start_cmd = format("export JAVA_HOME=" + params.java_home + "  && sh "+ params.install_dir +"/mlsql/mlsql-console.sh start")
        Execute(console_start_cmd,user=params.mlsql_user)
        # Execute('sh /opt/mlsql/mlsql-console/mlsql-console.sh start', user=params.mlsql_user)
        time.sleep(5)
        Execute('sh '+ params.install_dir +'/mlsql/mlsql-console.sh status', user=params.mlsql_user)

    def status(self, env):
        import status_params
        import params
        env.set_params(status_params)
        # check_process_status('/var/run/zookeeper/zookeeper_server.pid')
        check_process_status(status_params.mlsql_console_pid_file)

    def get_pid_files(self):
        import status_params
        return [status_params.mlsql_console_pid_file]

    def get_log_folder(self):
        import params
        return params.log_dir

    def get_user(self):
        import params
        return params.mlsql_user
          
if __name__ == "__main__":
    Console().execute()
